#include <iostream>
#include <queue>
#include <math.h>
using namespace std;

struct edge{
	int start,stop;
	int len,sa_next,so_next;
};

struct node{
	int len;
	char start;
	bool operator < (const node & y)const{
		return len>y.len;
	}
};


int main(){
	int p;
	cin>>p;
	bool tf[52];
	edge edges[p];
	int edge_header[52],fast[52];
	for(int i=0;i<52;i++){
		edge_header[i] = -1;
		tf[i] = false;
		fast[i] = 0x6fffffff;
	}
	for(int i=0;i<p;i++){
		register char start,stop;
		register int len;
		cin>>start>>stop>>len;
		start = start<='z' && start >= 'a' ? start-'a':start-'A'+26;
		stop = stop<='z' && stop >= 'a' ? stop-'a':stop-'A'+26;
		edges[i]={start, stop, len, edge_header[start], edge_header[stop]};
		tf[start]=true;
		tf[stop]=true;
		edge_header[start] = i;
		edge_header[stop] = i;
	}
//	for(int i=0;i<p;i++){
//		cout<<"edges["<<i<<"]: "<<edges[i].start<<" "<<edges[i].stop<<" "<<edges[i].len<<" "<<edges[i].sa_next<<" "<<edges[i].so_next<<endl;
//	}
//	for(int i=0;i<52;i++){
//		cout<<"edge_header["<<i<<"]: "<<edge_header[i]<<endl;
//	}
	int x=51;  //Z
	int len=0;
	node ll;
	priority_queue<node> q;
	for(int i=0;i<52;i++){
		int l=edge_header[x];
		while(l!=-1){
			if(edges[l].start==x){
				if(fast[edges[l].stop]>len+edges[l].len){
					fast[edges[l].stop] = len+edges[l].len;
					ll.len = fast[edges[l].stop];
					ll.start = edges[l].stop;
					q.push(ll);
				}
				l = edges[l].sa_next;
			}
			else{
				if(fast[edges[l].start]>len+edges[l].len){
					fast[edges[l].start] = len+edges[l].len;
					ll.len = fast[edges[l].start];
					ll.start = edges[l].start;
					q.push(ll);
				}
				l = edges[l].so_next;
			}
		}
		ll = q.top();
		if(ll.start>25&&ll.start!=51) break;
		x = ll.start;
//		cout<<"now x= "<<char(x+'a')<<endl;
		while(!tf[x]){
			q.pop();
			ll = q.top();
			x = ll.start;
//			cout<<"now x= "<<char(x<26?x+'a':x-26+'A')<<endl;
		}
		len = ll.len;
		tf[x] = false;
		q.pop();
	}
	
	
	cout<<char(ll.start-26+'A')<<' '<<ll.len;
	return 0;
}
